package com.camel.base.config;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author camel
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private SaTokenDao saTokenDao;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String permissionStr = saTokenDao.get("x-token:login:permission:" + StpUtil.getTokenValue());
        if (StringUtils.isEmpty(permissionStr)) {
            return new ArrayList<>();
        }
        return CollectionUtils.arrayToList(permissionStr.split(","));
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String roleStr = saTokenDao.get("x-token:login:role:" + StpUtil.getTokenValue());
        if (StringUtils.isEmpty(roleStr)) {
            return new ArrayList<>();
        }
        return CollectionUtils.arrayToList(roleStr.split(","));
    }
}
